package cn.xzqwjw.taskmanager.service.impl;

import cn.xzqwjw.taskmanager.common.customEnum.ResponseCodeEnum;
import cn.xzqwjw.taskmanager.common.exception.CustomException;
import cn.xzqwjw.taskmanager.domain.pojo.TaskDuty;
import cn.xzqwjw.taskmanager.domain.vo.ResponseVo;
import cn.xzqwjw.taskmanager.domain.vo.TaskDutyVo;
import cn.xzqwjw.taskmanager.service.TaskDutyService;
import cn.xzqwjw.taskmanager.repository.TaskDutyMapper;
import cn.xzqwjw.taskmanager.utils.ConvertUtil;
import cn.xzqwjw.taskmanager.utils.JsonUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wjw
 */
@Service("TaskDutyService")
@Slf4j
public class TaskDutyServiceImpl extends ServiceImpl<TaskDutyMapper, TaskDuty> implements TaskDutyService {

  @Transactional
  @Override
  public String add(TaskDutyVo taskDutyVo) {
    int idTask = taskDutyVo.getIdTask();
    Map<String, String> paramJsonMap = (Map<String, String>) JsonUtil.json2Map(taskDutyVo.getParamJsonMap());
    // 传入的数据格式如下：
    // const map = new Map()
    // map.set('Leader', leaderIds)
    // map.set('Stuff', staffIds)
    // map.set('Department', departmentIds)
    if (!paramJsonMap.containsKey("Leader") ||
        !paramJsonMap.containsKey("Stuff") ||
        !paramJsonMap.containsKey("Department")) {
      throw new CustomException(ResponseCodeEnum.BAD_REQUEST);
    }
    // 添加责任领导
    boolean result = addBatch(idTask, "Leader", paramJsonMap.get("Leader"));
    if (!result) {
      throw new CustomException("批量添加责任领导出错！");
    }
    // 添加责任人员
    result = addBatch(idTask, "Stuff", paramJsonMap.get("Stuff"));
    if (!result) {
      throw new CustomException("批量添加责任人员出错！");
    }
    // 添加责任科室
    result = addBatch(idTask, "Department", paramJsonMap.get("Department"));
    if (!result) {
      throw new CustomException("批量添加责任科室出错！");
    }
    return JsonUtil.obj2Json(ResponseVo.success(ResponseCodeEnum.OK_CREATED));
  }


  @Override
  public String delById(int id) {
    if (removeById(id)) {
      return JsonUtil.obj2Json(ResponseVo.success(ResponseCodeEnum.OK_DELETED));
    } else {
      throw new CustomException(ResponseCodeEnum.ERROR_DELETE);
    }
  }

  @Override
  public String listAll(int idTask) {
    QueryWrapper<TaskDuty> wrapper = new QueryWrapper<>();
    wrapper.eq("id_task", idTask);
    return JsonUtil.obj2Json(ResponseVo.success(list(wrapper)));
  }

  // ==================== 私有函数方法 ====================

  /***
   * 批量添加责任相关数据
   */
  @Transactional
  public boolean addBatch(int idTask, String type, String ids) {
    List<TaskDuty> tdList = new ArrayList<>();
    TaskDuty taskDuty;
    int id;
    for (String idString : ids.split(",")) {
      taskDuty = new TaskDuty();
      taskDuty.setIdTask(idTask);
      taskDuty.setIdUnit(106);
      taskDuty.setDtCreate(LocalDateTime.now());
      taskDuty.setDutyType(type);

      id = ConvertUtil.str2Int(idString);
      if ("Department".equals(type)) {
        taskDuty.setIdDepartment(id);
        taskDuty.setIdAdmin(0);
      } else {
        taskDuty.setIdDepartment(0);
        taskDuty.setIdAdmin(id);
      }

      tdList.add(taskDuty);
    }
    return saveBatch(tdList);
  }

}




